package app.server.project.main.service.controller;

import app.server.project.core.constants.Def;
import app.server.project.core.exception.SysError;
import app.server.project.core.http.CMSLstResp;
import app.server.project.core.http.RespModel;
import app.server.project.core.httpauth.UnAuth;
import app.server.project.core.token.TokenCache;
import app.server.project.core.util.AssertUtil;
import app.server.project.main.service.dto.recharge.*;
import app.server.project.main.service.service.recharge.RechargeCfgService;
import app.server.project.main.service.service.recharge.RechargeService;
import app.server.project.main.service.service.recharge.StarRecharge;
import app.server.project.main.service.service.recharge.TONRecharge;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

/**
 * 充值模块
 */
@AllArgsConstructor
@RestController
@RequestMapping("/recharge")
public class RechargeController {
    private RechargeService rechargeService;
    private RechargeCfgService rechargeCfgService;
    private TONRecharge tonRecharge;
    private StarRecharge starRecharge;

    /**
     * 充值列表 1 aicoin 2 star 3 ton
     *
     * @return
     */
    @PostMapping("/getRecharge")
    public RespModel<RechargeDto> getRecharge(@RequestBody QueryRechargeReq req, @RequestHeader(name = Def.Lang, defaultValue = "en") String lang) {
        return RespModel.success(this.rechargeService.getRecharge(req.getTypeId(), lang));
    }


    /**
     * 拉取全部充值列表
     *
     * @return
     */
    @PostMapping("/getAll")
    public RespModel<RechargeResp> getAll() {
        return RespModel.success(this.rechargeService.getAll());
    }

    /**
     * 充值star
     *
     * @param req
     * @param userId
     * @return
     */
    @PostMapping("/buyStar")
    public RespModel buyStar(@RequestBody BuyStarReq req, @RequestHeader(Def.UserId) long userId) {
        this.starRecharge.buy(req, userId);
        return RespModel.success();
    }


    /**
     * 充值ton
     *
     * @param req
     * @param userId
     * @return
     */
    @PostMapping("/buyTon")
    public RespModel<TONOrderDto> buyTon(@RequestBody BuyTonReq req, @RequestHeader(Def.UserId) long userId) {
        return RespModel.success(this.tonRecharge.createTonOrder(req, userId));
    }

    /**
     * 查询订单
     *
     * @param req
     * @return
     */
    @RequestMapping("/getAllOrder")
    public CMSLstResp<RechargeOrderDto> getAllOrder(QueryRechargeOrderReq req) {
        return this.rechargeService.getAllOrder(req);
    }

    @RequestMapping("/humanRecharge")
    public RespModel humanRecharge(@RequestBody HumanReq dto) {
        this.rechargeService.humanRecharge(dto);
        return RespModel.success();
    }

    /**
     * 上传客户端代码
     *
     * @param req
     */
    @UnAuth
    @PostMapping("/uploadImg/{userId}/{token}/{rechargeId}")
    public RespModel uploadImg(@RequestParam("Fdata") MultipartFile files, @PathVariable int rechargeId, @PathVariable long userId, @PathVariable String token) {
        AssertUtil.isTrue(TokenCache.chkToken(userId, token), SysError.ErrorToken);
        this.rechargeCfgService.uploadImg(files, rechargeId);
        return RespModel.success();
    }

    /**
     * 上传客户端代码
     *
     * @param req
     */
    @UnAuth
    @PostMapping("/uploadSku/{userId}/{token}/{rechargeId}")
    public RespModel uploadSku(@RequestParam("Fdata") MultipartFile files, @PathVariable int rechargeId, @PathVariable long userId, @PathVariable String token) {
        AssertUtil.isTrue(TokenCache.chkToken(userId, token), SysError.ErrorToken);
        this.rechargeCfgService.uploadSku(files, rechargeId);
        return RespModel.success();
    }


    /**
     * ton主动关闭订单
     *
     * @param req
     */
    @PostMapping("/clientCloseOrder")
    public RespModel clientCloseOrder(@RequestBody ClientCloseReq req) {
        this.tonRecharge.clientCloseOrder(req);
        return RespModel.success();
    }
}
